﻿using System;
using System.Data.SqlClient;
using System.Text;


namespace NetCoreTest
{
    public class DbConnectionLab
    {
        public static async void ReadDbSync()
        {

            const int tableWidth = 15;

            SqlConnection conn = new SqlConnection("Data Source=<domain>;Initial Catalog=<dbname>;User ID=<username>;Password=<password>");

            conn.Open();
            SqlCommand command = new SqlCommand("SELECT * FROM iNethinkCMS_User", conn);

            SqlDataReader sdr = command.ExecuteReader();

            StringBuilder EchoBuffer = new StringBuilder(string.Empty);
            for (int i = 0; i < sdr.FieldCount; i++)
            {
                EchoBuffer.AppendFormat("{0}", sdr.GetName(i).PadLeft(tableWidth));
            }
            Console.WriteLine(EchoBuffer.ToString());
            EchoBuffer.Clear();
            while (await sdr.ReadAsync())
            {

                for (int i = 0; i < sdr.FieldCount; i++)
                {
                    string value = sdr[i].ToString();
                    value = value.Length > tableWidth - 2 ? value.Substring(0, tableWidth - 2) : value;
                    EchoBuffer.Append($"{value.PadLeft(tableWidth)}");
                }
                Console.WriteLine(EchoBuffer.ToString());
                EchoBuffer.Clear();
            }

            conn.Close();
        }
    }
}
